/**
 * 
 */
package de.akabahn.ebd.fst.control;

import org.apache.log4j.Logger;

/**
 * Launcher class that starts the <code>FahrSteuerungControl</code> application.
 * 
 * @author Peter Eimann
 * @since 1.0
 * @version 1.0
 * @see FahrSteuerungControl
 */
public class FahrSteuerungControlLauncher {

	public final static String VERSION = "1.5.0_5";
	private final static Logger LOG = Logger.getLogger(FahrSteuerungControlLauncher.class);

	private static int port;
	private static int checkInterval;
	private static boolean test;
	private static String fstServer;
	private static int fstPort;

	/**
	 * Parse the passed arguments and start the
	 * <code>FahrSteuerungControl</code> application<br>
	 * <br>
	 * <code>
	 * FahrSteuerungControlLauncher &lt;arguments&gt;<br>
	 * -l: port to listen for incoming requests<br>
	 * -i: check interval in ms<br>
	 * -t: test mode flag (boolean)<br>
	 * -s: ip-address or name of the Fahrsteuerung server (optional if the default value shouldn't be used)<br>
	 * -p: port of the Fahrsteuerung server (optional if the default value shouldn't be used)
	 * </code>
	 * 
	 * @param args
	 *            The program arguments
	 * @see #resolveArguments(String, String)
	 */
	public static void main(String[] args) {
		FahrSteuerungControl control = null;

		if ((args.length > 0) && (args.length % 2 == 0)) {
			for (int index = 0; index < args.length; index += 2) {
				resolveArguments(args[index], args[index + 1]);
			}

			try {
				control = new FahrSteuerungControl(port, checkInterval, test, fstServer, fstPort);
				LOG.info("FahrSteuerungControl " + VERSION + " started");
				LOG.info("Local port: " + control.getPort());
				LOG.info("Check interval: " + control.getCheckInterval());
				LOG.info("Test mode: " + control.isTest());
				LOG.info("Fst server: " + control.getFstServer());
				LOG.info("Fst port: " + control.getFstPort());
				control.start();
				System.exit(0);
			} catch (Throwable t) {
				t.printStackTrace();
				System.exit(2);
			}
		} else {
			printUsage();
			System.exit(1);
		}
	}

	/**
	 * Resolve the passed program arguments, supported names are:
	 * <ul>
	 * <li><code>-l</code> for the TCP port to listen</li>
	 * <li><code>-i</code> for the check interval</li>
	 * <li><code>-t</code> for the test flag</li>
	 * <li><code>-s</code> for the Fahrsteuerung server</li>
	 * <li><code>-p</code> for the Fahrsteuerung port</li>
	 * </ul>
	 * 
	 * @param name
	 *            The name of the parameter
	 * @param value
	 *            The value of the parameter
	 */
	private static void resolveArguments(String name, String value) {
		if ("-l".equalsIgnoreCase(name)) {
			port = Integer.parseInt(value);
		} else if ("-i".equalsIgnoreCase(name)) {
			checkInterval = Integer.parseInt(value);
		} else if ("-t".equalsIgnoreCase(name)) {
			test = Boolean.parseBoolean(value);
		} else if ("-s".equalsIgnoreCase(name)) {
			fstServer = value;
		} else if ("-p".equalsIgnoreCase(name)) {
			fstPort = Integer.parseInt(value);
		}
	}

	/**
	 * Print out the supported program arguments
	 */
	private static void printUsage() {
		System.err.println("Usage:\n");
		System.err.println("FahrSteuerungControlLauncher -p <port> -i <checkInterval> -t <test mode>\n");
		System.err.println("-l: port to listen for incoming requests");
		System.err.println("-i: check interval in ms");
		System.err.println("-t: run in test mode (true)");
		System.err.println("-s: ip-address or name of the Fahrsteuerung server (optional if the default value shouldn't be used)");
		System.err.println("-p: port of the Fahrsteuerung server (optional if the default value shouldn't be used)");
	}
}
